package com.example.su_shi_tan_bas_lib.view.video_view.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;

import androidx.transition.TransitionManager;

import com.example.su_shi_tan_bas_lib.utils.QMUIDisplayHelper;
import com.example.su_shi_tan_bas_lib.utils.StatusBar;
import com.example.su_shi_tan_bas_lib.view.video_view.CustomVideoView;

public class VideoScreenSwitchManager {

    private int[] mListItemRect = new int[2];

    private int[] mListItemSize = new int[2];

    private int mSystemUiVisibility;

    private final static class ManagerHolder {
        private final static VideoScreenSwitchManager HOLDER = new VideoScreenSwitchManager();
    }

    public static VideoScreenSwitchManager getInstance() {
        return ManagerHolder.HOLDER;
    }

    private VideoScreenSwitchManager() {
    }

    private FinalTaskEvent finalTaskEvent;

    public VideoScreenSwitchManager setFinalTaskEvent(FinalTaskEvent finalTaskEvent) {
        this.finalTaskEvent = finalTaskEvent;
        return this;
    }

    //全屏
    public VideoScreenSwitchManager toChangLandscape(CustomVideoView normal) {

        Context context = normal.getContext();

        mSystemUiVisibility = ((Activity) context).getWindow().getDecorView().getSystemUiVisibility();

        VideoViewUtils.hideSupportActionBar(context);

        VideoViewUtils.hideNavKey(context);

        final ViewGroup mWindowViewGroup = VideoViewUtils.getWindowViewGroup(context);

        removeVideo(mWindowViewGroup, VideoViewUtils.getFullId());

        saveLocationStatus(normal);

        View renderContainer = normal.getRenderContainer();

        normal.removeAllViews();

        ViewParent parent = renderContainer.getParent();
        if (parent instanceof ViewGroup) {
            ((ViewGroup) parent).removeAllViews();
        }

        renderContainer.setId(VideoViewUtils.getFullId());


        final FrameLayout.LayoutParams lpParent = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        final FrameLayout frameLayout = new FrameLayout(context);
        frameLayout.setBackgroundColor(Color.BLACK);

        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(normal.getPrimaryWidth(), normal.getPrimaryHeight());
        lp.setMargins(mListItemRect[0], mListItemRect[1], 0, 0);

        frameLayout.addView(renderContainer, lp);

        mWindowViewGroup.addView(frameLayout, lpParent);

        normal.postDelayed(() -> {
            TransitionManager.beginDelayedTransition(mWindowViewGroup);
            resolveFullVideoShow(normal, renderContainer);
        }, 300);

        return this;
    }

    private void resolveFullVideoShow(CustomVideoView normal, View renderContainer) {
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) renderContainer.getLayoutParams();
        lp.setMargins(0, 0, 0, 0);
        lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.gravity = Gravity.CENTER;
        renderContainer.setLayoutParams(lp);
        renderContainer.postDelayed(() -> resolveFinalTask(normal), 300);

    }

    private void resolveFinalTask(CustomVideoView normal) {
        if (finalTaskEvent != null) {
            finalTaskEvent.onHandle();
            finalTaskEvent = null;
        }
    }

    private void saveLocationStatus(CustomVideoView normal) {
        normal.getLocationOnScreen(mListItemRect);
        Context context = normal.getContext();
        if (context instanceof Activity) {
            int statusBarH = QMUIDisplayHelper.getStatusBarHeight(context);
            int actionBerH = QMUIDisplayHelper.getActionBarHeight(context);

            if (StatusBar.isTranslucent()) {
                statusBarH = 0;
            }
            if (mListItemRect[1] - statusBarH >= 0) {
                mListItemRect[1] = mListItemRect[1] - statusBarH;
            }

            if (QMUIDisplayHelper.hasNavigationBar(context)) {
                mListItemRect[1] = mListItemRect[1] - actionBerH;
            }

        }

        mListItemSize[0] = normal.getPrimaryWidth();
        mListItemSize[1] = normal.getPrimaryHeight();
    }

    private void removeVideo(ViewGroup vp, int id) {
        View old = vp.findViewById(id);
        if (old != null) {
            if (old.getParent() != null) {
                ViewGroup viewGroup = (ViewGroup) old.getParent();
                vp.removeView(viewGroup);
            }
        }
    }


    //恢复
    public VideoScreenSwitchManager toChangPortrait(CustomVideoView normal) {

        preSetRenderContainerLayoutParams(normal);

        normal.postDelayed(() -> backToNormal(normal), 500);

        return this;
    }

    public interface FinalTaskEvent {
        void onHandle();
    }

    private void preSetRenderContainerLayoutParams(CustomVideoView normal) {

        Context context = normal.getContext();
        final ViewGroup mWindowViewGroup = VideoViewUtils.getWindowViewGroup(context);
        final View renderContainer = mWindowViewGroup.findViewById(VideoViewUtils.getFullId());

        FrameLayout.LayoutParams layoutParams =
                (FrameLayout.LayoutParams) renderContainer.getLayoutParams();

        if (!StatusBar.isTranslucent()) {
            layoutParams.setMargins(mListItemRect[0], mListItemRect[1], 0, 0);
        }

        layoutParams.width = normal.getPrimaryWidth();
        layoutParams.height = normal.getPrimaryHeight();

        layoutParams.gravity = Gravity.CENTER;
    }

    private void backToNormal(CustomVideoView normal) {
        Context context = normal.getContext();
        final ViewGroup mWindowViewGroup = VideoViewUtils.getWindowViewGroup(context);
        final View renderContainer = mWindowViewGroup.findViewById(VideoViewUtils.getFullId());

        TransitionManager.beginDelayedTransition(mWindowViewGroup);
//
        FrameLayout.LayoutParams layoutParams =
                (FrameLayout.LayoutParams) renderContainer.getLayoutParams();

        //注意配置回来，不然动画效果会不对
        layoutParams.gravity = Gravity.NO_GRAVITY;

        renderContainer.setLayoutParams(layoutParams);

        normal.postDelayed(() -> resolveNormalVideoShow(renderContainer, mWindowViewGroup, normal), 500);

        VideoViewUtils.showNavKey(context, mSystemUiVisibility);
        VideoViewUtils.showSupportActionBar(context);


    }


    private void resolveNormalVideoShow(View renderContainer, ViewGroup mWindowViewGroup, CustomVideoView normal) {
        if (renderContainer == null) {
            return;
        }
        ViewParent renderParent = renderContainer.getParent();
        if (renderParent instanceof ViewGroup) {

            ViewGroup renderViewGroupParent = (ViewGroup) renderParent;
            mWindowViewGroup.removeView(renderViewGroupParent);
            renderViewGroupParent.removeView(renderContainer);
        }
        renderContainer.setId(normal.getVideoViewRenderContainerId());
        normal.addView(renderContainer);
        resolveFinalTask(normal);
    }

}
